package controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.RezyserEntity;

/**
 * Servlet implementation class RezyserAdminAdd
 */
@WebServlet("/RezyserAdmin")
public class RezyserAdmin extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RezyserAdmin() {
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String add = request.getParameter("add");
		if(add!=null)
		{
			if(Boolean.parseBoolean(add))	// dodawanie
			{
				RequestDispatcher dispatcher = 
			    		getServletContext().getRequestDispatcher("/restricted/dodaj_rezysera.jsp"); 
			    dispatcher.forward(request, response);
			}
			else
			{
				String id = request.getParameter("id");
				
				if(id!=null)
				{
					Integer id_R = new Integer(id); 
					String isEdit = request.getParameter("edit");
					if(Boolean.parseBoolean(isEdit))	// edycja
					{
						try {
							OracleProcedure op = new OracleProcedure(OracleProcedure.SELECT,"Rezyser");
							op.setColumn("id");
							op.make(true,id_R);
							ResultSet cursor = op.getCursor();
							cursor.next();
							request.setAttribute("Rezyser", new RezyserEntity(id_R,cursor.getString("imie"),cursor.getString("nazwisko"))); 
						
						} catch (SQLException e) {
							e.printStackTrace();
						}
						RequestDispatcher dispatcher = 
					    		getServletContext().getRequestDispatcher("/restricted/edytuj_rezysera.jsp"); 
					    dispatcher.forward(request, response);
					}
					else	// usuniecie
					{
						OracleProcedure op = new OracleProcedure(OracleProcedure.DELETE,"Rezyser");
						op.make(id_R);
						processRequest(request,response);
					}
				}
				else	// wyswietlanie
				{
					OracleProcedure op = new OracleProcedure(OracleProcedure.SELECT,"Rezyser");
					op.make(true);
					ResultSet cursor = op.getCursor();
					request.setAttribute("RezyserList",
							Utils.prepareFullRezyserList(cursor));

					RequestDispatcher dispatcher = 
				    		getServletContext().getRequestDispatcher("/restricted/pokaz_rezysera.jsp"); 
				    dispatcher.forward(request, response);
				}
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String imie = (String) request.getParameter("rezyser_imie");
			String nazwisko = (String) request.getParameter("rezyser_nazwisko");
			String id = (String) request.getParameter("id");
			if(id!=null)	// update
			{
				Integer id_r = new Integer(id);
				OracleProcedure op = new OracleProcedure(OracleProcedure.UPDATE,"Rezyser");
				op.make(id_r,imie,nazwisko);
			}
			else	// insert
			{
				OracleProcedure op = new OracleProcedure(OracleProcedure.INSERT,"Rezyser");
				op.make(imie,nazwisko);
			}
			
			processRequest(request,response);
		} catch (Exception ex) {
			throw new ServletException(ex);
		} finally {
		}
	}

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
		    throws ServletException {
		try {
			response.sendRedirect(getServletContext().getContextPath()+"/restricted/FilmAdmin");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

}
